package com.hospital.controller;

import com.hospital.constant.MessageConstant;
import com.hospital.dto.PatientPageQueryDTO;
import com.hospital.dto.PatientRegisterDTO;
import com.hospital.result.PageResult;
import com.hospital.result.Result;
import com.hospital.service.PatientService;
import com.hospital.vo.DischargeInfoVO;
import com.hospital.vo.PatientGenderVO;
import com.hospital.vo.PatientInfoVO;
import com.hospital.vo.PatientStatsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/patient")
@Slf4j
@Api(tags = "2.病人管理")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @PostMapping("/addPatient")
    @ApiOperation("添加病人信息")
    @Transactional
    public Result<String> addPatient(@RequestBody @Valid PatientRegisterDTO patientRegisterDTO) {
        log.info("addPatient: {}", patientRegisterDTO);
        patientService.addPatient(patientRegisterDTO);

        return Result.success(MessageConstant.PATIENT_IS_IN_HOSPITAL);
    }

    @GetMapping("/getDischargeInfo/{patientId}")
    @ApiOperation("获取病人出院信息")
    public Result<DischargeInfoVO> getDischargeInfo(@PathVariable("patientId") Long patientId) {
        log.info("病人id: {}", patientId);

        DischargeInfoVO dischargeInfoVO = patientService.getDischargeInfo(patientId);
        return Result.success(dischargeInfoVO, MessageConstant.GET_DISCHARGE_INFO);
    }

    @GetMapping("/discharge/{patientId}")
    @ApiOperation("病人出院")
    @Transactional
    public Result<String> discharge(@PathVariable("patientId") Long patientId) {
        log.info("病人id: {}", patientId);

        patientService.discharge(patientId);
        return Result.success(MessageConstant.DISCHARGE_SUCCESS);
    }

    @GetMapping("/getPatientList")
    @ApiOperation("获取病人列表")
    public Result<PageResult> getPatientList(PatientPageQueryDTO patientPageQueryDTO){
        log.info("病人分页查询参数: {}", patientPageQueryDTO);
        PageResult pageResult = patientService.getPatientListPage(patientPageQueryDTO);
        return Result.success(pageResult, MessageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/getPatientInfo/")
    @ApiOperation("获取病人信息")
    public Result<PatientInfoVO> getPatientInfo(Long patientId){
        log.info("病人id: {}", patientId);
        PatientInfoVO patientInfoVO = patientService.getPatientInfo(patientId);
        return Result.success(patientInfoVO, MessageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/getPatientStats")
    @ApiOperation("获取病人统计信息")
    public Result<PatientStatsVO> getPatientStats(){
        log.info("获取病人统计信息");
        PatientStatsVO patientStatsVO = patientService.getPatientStats();
        return Result.success(patientStatsVO, MessageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/getPatientGender")
    @ApiOperation("获取病人性别比例")
    public Result<List<PatientGenderVO>> getPatientGender(){
        log.info("获取病人性别比例");
        List<PatientGenderVO> patientGenderVOList = patientService.getPatientGender();
        return Result.success(patientGenderVOList, MessageConstant.QUERY_SUCCESS);
    }
}
